/**
 * 
 */
package gui.view;

import gui.controller.ServicesPanelListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableCellRenderer;

import stubs.ProbabilityDistribution;
import stubs.TrustLevel;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * @author Elisa Costante
 * 
 */
public class ServicesPanel extends JPanel {

	private static final long serialVersionUID = -170106123579104737L;

	private JPanel leftPanel;

	private JLabel numberOfServicesLabel;
	private JComboBox numberOfServicesCombo;
	private JScrollPane tableCartesianScrollPane;
	private JTable servicesCartesianTable;
	private JLabel probDistrLabel;
	private JComboBox probDistrCombo;
	private JLabel trustLevelLabel;
	private JComboBox trustLevelCombo;
	private CartesianServicesTableModel cartesianTableModel;

	private ServicesPanelListener listener;

	private JButton generateButton;

	private EngineMain frame;

	private JTextArea servicesExplanationTxtArea;

	private JButton loadButton;

	private JButton saveButton;

	private JScrollPane servicesExplanationScroll;

	private JDesktopPane desktopPane;

	private JInternalFrame internalFrame;

	private InputPanel inputPanel;

	public ServicesPanel(EngineMain frame) {
		super();
		this.frame = frame;
		initPanel();
	}

	private void initPanel() {
		this.setBackground(CustomizedColor.BACK_GROUND_COLOR);
		listener = new ServicesPanelListener(this);
		setComponents();
		setLeftPanel();

		createJinternalPane();

		setMainPanelLayout();

	}

	/**
	 * 
	 */
	public void createJinternalPane() {
		desktopPane = new JDesktopPane();
		desktopPane.setBackground(CustomizedColor.BACK_GROUND_COLOR);

		internalFrame = new JInternalFrame("Input", true, false, true);
		internalFrame.setBounds(20, 20, EngineMain.MAIN_WIDTH - 50,
				EngineMain.MAIN_HEIGHT - 100);
		internalFrame.setBackground(CustomizedColor.BACK_GROUND_COLOR);

		this.add(desktopPane);

	}

	public void closeInternalFrame() {
		desktopPane.setVisible(false);
		internalFrame.setVisible(false);
		internalFrame.setBounds(20, 20, EngineMain.MAIN_WIDTH - 50,
				EngineMain.MAIN_HEIGHT - 100);

		this.setMainPanelLayout();

		leftPanel.setVisible(true);

	}

	public void openInternalFrame(InputPanel inputPanel) {
		this.inputPanel = inputPanel;
		leftPanel.setVisible(false);

		// Remove old components
		internalFrame.getContentPane().removeAll();
		desktopPane.removeAll();

		this.setLayout(new BorderLayout());
		desktopPane.add(internalFrame, BorderLayout.CENTER);
		internalFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);
		this.add(desktopPane);

		desktopPane.setVisible(true);
		internalFrame.setVisible(true);

	}

	private void setComponents() {

		setLeftComponents();

	}

	private void setLeftComponents() {
		numberOfServicesLabel = new JLabel("Number of Services");

		numberOfServicesCombo = new JComboBox(new Integer[] { 2, 3, 4, 5, 6, 7,
				8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 });
		numberOfServicesCombo.setActionCommand("NumberOfServices");
		numberOfServicesCombo.addActionListener(listener);

		probDistrLabel = new JLabel("Probability Distributions");
		probDistrCombo = new JComboBox(new String[] {
				ProbabilityDistribution.NORMAL.getLabel(),
				ProbabilityDistribution.POISSON.getLabel(),
				ProbabilityDistribution.BERNOULLI.getLabel() });
		probDistrCombo.setSelectedIndex(0);
		probDistrCombo.setActionCommand("probabilityChange");
		probDistrCombo.addActionListener(listener);

		trustLevelLabel = new JLabel("Trust Level");
		trustLevelCombo = new JComboBox(new String[] { "NULL",
				TrustLevel.LOW.getLabel(), TrustLevel.MEDIUM.getLabel(),
				TrustLevel.HIGH.getLabel() });
		trustLevelCombo.setSelectedIndex(0);

		tableCartesianScrollPane = new JScrollPane(getCartesianServicesTable());
		getCartesianServicesTable().setFillsViewportHeight(true);

		servicesExplanationTxtArea = new JTextArea();
		servicesExplanationTxtArea.setEditable(false);
		servicesExplanationTxtArea.setFont(new Font("Serif", Font.PLAIN, 12));

		Border border = BorderFactory.createLineBorder(Color.gray);
		border = BorderFactory.createTitledBorder(border,
				"How to use services Settings", TitledBorder.CENTER,
				TitledBorder.CENTER);
		servicesExplanationTxtArea.setBorder(border);
		servicesExplanationTxtArea
				.setText("Select the number of service in order to decide how many services the monitor has monitored in the past.\n\n"
						+ "The 'Probability disrtibution' selection is used to generate value for the metric relative to the measurement\n "
						+ "of the different attributes that follow the given probability (Normal, Poisson,Bernoulli).\n\n"
						+ "The underneath table has the aim to set the previous interactions"
						+ " between si and sj.\n If the cell [si, sj] where 'i' is the row"
						+ " and 'j' is the column has the NULL value that means the service si"
						+ " has never called the service sj in the past.\n"
						+ "Otherwise the cell has a value that can be Highly, Medium or Low Trustable. That means that the previus interactions between si and sj have been more o less trustable.\n");

		servicesExplanationScroll = new JScrollPane(servicesExplanationTxtArea);

		loadButton = new JButton("Load");
		loadButton.setActionCommand("load");
		loadButton.addActionListener(this.listener);

		saveButton = new JButton("Save");
		saveButton.setActionCommand("save");
		saveButton.addActionListener(this.listener);

		generateButton = new JButton("Generate data");
		generateButton.setActionCommand("generateData");
		generateButton.addActionListener(listener);

	}

	/**
	 * @return
	 */
	private JTable getCartesianServicesTable() {
		if (servicesCartesianTable == null) {
			cartesianTableModel = new CartesianServicesTableModel();
			servicesCartesianTable = new JTable(cartesianTableModel);

			// Set the CellRender
			TableCellRenderer renderer = new CustomTableCellRenderer();

			servicesCartesianTable.setDefaultRenderer(String.class, renderer);

		}
		return servicesCartesianTable;
	}

	public void setLeftPanel() {

		leftPanel = new JPanel();
		leftPanel.setBackground(CustomizedColor.BACK_GROUND_COLOR);
		setLeftPanelLayout();
	}

	/**
	 * 
	 */
	private void setLeftPanelLayout() {

		GroupLayout layout = new GroupLayout(leftPanel);
		leftPanel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		ParallelGroup horizontal = layout.createParallelGroup();

		SequentialGroup upGroup = layout.createSequentialGroup();
		upGroup.addComponent(numberOfServicesLabel, 100, 200, Short.MAX_VALUE);
		upGroup.addComponent(numberOfServicesCombo, 200, 300, Short.MAX_VALUE);
		upGroup.addComponent(probDistrLabel, 100, 200, Short.MAX_VALUE);
		upGroup.addComponent(probDistrCombo, 200, 300, Short.MAX_VALUE);

		horizontal.addGroup(upGroup);
		horizontal.addComponent(servicesExplanationScroll, 200, 200,
				Short.MAX_VALUE);
		horizontal.addComponent(tableCartesianScrollPane, 200, 300,
				Short.MAX_VALUE);

		SequentialGroup buttonsHorGroup = layout.createSequentialGroup();

		buttonsHorGroup.addComponent(generateButton, 100, 100, 150);
		buttonsHorGroup.addComponent(loadButton, 100, 100, 100);
		buttonsHorGroup.addComponent(saveButton, 100, 100, 100);
		horizontal.addGroup(buttonsHorGroup);

		SequentialGroup vertical = layout.createSequentialGroup();

		ParallelGroup leftVerticalGroup = layout.createParallelGroup();
		leftVerticalGroup.addComponent(numberOfServicesLabel, 30, 30, 30);
		leftVerticalGroup.addComponent(numberOfServicesCombo, 30, 30, 30);
		leftVerticalGroup.addComponent(probDistrLabel, 30, 30, 30);
		leftVerticalGroup.addComponent(probDistrCombo, 30, 30, 30);

		vertical.addGroup(leftVerticalGroup);
		vertical.addComponent(servicesExplanationScroll, 100, 130, 150);
		vertical.addComponent(tableCartesianScrollPane, 200, 300,
				Short.MAX_VALUE);

		ParallelGroup buttonsVertGroup = layout
				.createParallelGroup(Alignment.CENTER);
		buttonsVertGroup.addComponent(generateButton, 30, 30, 30);
		buttonsVertGroup.addComponent(loadButton, 30, 30, 30);
		buttonsVertGroup.addComponent(saveButton, 30, 30, 30);

		vertical.addGroup(buttonsVertGroup);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);

	}

	private void setMainPanelLayout() {
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		SequentialGroup horizontal = layout.createSequentialGroup();

		ParallelGroup leftGroup = layout.createParallelGroup();
		leftGroup.addComponent(leftPanel, this.getWidth() / 2,
				this.getWidth() / 2, Short.MAX_VALUE);

		horizontal.addGroup(leftGroup);

		SequentialGroup vertical = layout.createSequentialGroup();

		ParallelGroup leftVerticalGroup = layout.createParallelGroup();
		leftVerticalGroup.addComponent(leftPanel, this.getHeight() / 2, this
				.getHeight() / 2, Short.MAX_VALUE);

		vertical.addGroup(leftVerticalGroup);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	/**
	 * @return the leftPanel
	 */
	public JPanel getLeftPanel() {
		return this.leftPanel;
	}

	/**
	 * @return the numberOfServicesLabel
	 */
	public JLabel getNumberOfServicesLabel() {
		return this.numberOfServicesLabel;
	}

	/**
	 * @return the numberOfServicesCombo
	 */
	public JComboBox getNumberOfServicesCombo() {
		return this.numberOfServicesCombo;
	}

	/**
	 * @return the tableCartesianScrollPane
	 */
	public JScrollPane getTableCartesianScrollPane() {
		return this.tableCartesianScrollPane;
	}

	/**
	 * @return the servicesCartesianTable
	 */
	public JTable getServicesCartesianTable() {
		return this.servicesCartesianTable;
	}

	/**
	 * @return the probDistrLabel
	 */
	public JLabel getProbDistrLabel() {
		return this.probDistrLabel;
	}

	/**
	 * @return the probDistrCombo
	 */
	public JComboBox getProbDistrCombo() {
		return this.probDistrCombo;
	}

	/**
	 * @return the trustLevelLabel
	 */
	public JLabel getTrustLevelLabel() {
		return this.trustLevelLabel;
	}

	/**
	 * @return the trustLevelCombo
	 */
	public JComboBox getTrustLevelCombo() {
		return this.trustLevelCombo;
	}

	/**
	 * @return the cartesianTableModel
	 */
	public CartesianServicesTableModel getCartesianTableModel() {
		return this.cartesianTableModel;
	}

	/**
	 * @return the frame
	 */
	public EngineMain getFrame() {
		return this.frame;
	}

	/**
	 * @return the inputPanel
	 */
	public InputPanel getInputPanel() {
		return this.inputPanel;
	}

}
